package com.luanhj.framework.tenant.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.luanhj.framework.tenant.TenantContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author luanhj
 * @date 2025/6/3 16:28
 * @description 自动填充 TenantId handler
 **/
@Component
public class MyMetaObjectHandler  implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println("🔍 正在执行自动填充...");
        if (metaObject.hasSetter("tenantId")) {
            String tenantId = TenantContext.getTenantId();
            System.out.println("🔐 当前租户ID：" + tenantId);
            if (tenantId != null) {
                this.strictInsertFill(metaObject, "tenantId", () -> tenantId, String.class);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新逻辑
    }
}
